package com.xmall.pethouse_api.controller;

import com.xmall.pethouse_api.pojo.Result;
import com.xmall.pethouse_api.pojo.entity.Order;
import com.xmall.pethouse_api.pojo.vo.OrderVO;
import com.xmall.pethouse_api.service.OrderService;
import com.xmall.pethouse_api.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private OrderService orderService;

    // 查询当前登录用户的所有订单（联表VO）
    @GetMapping("/list")
    public Result<List<OrderVO>> getMyOrders() {
        Map<String, Object> userMap = ThreadLocalUtil.get();
        Long userId = Long.valueOf(userMap.get("id").toString());
        List<OrderVO> orders = orderService.findOrderVOByUserId(userId);
        return Result.success(orders);
    }

    @PostMapping("/create")
    public Result<Order> createOrder(@RequestBody Order order) {
        Map<String, Object> userMap = ThreadLocalUtil.get();
        Long userId = Long.valueOf(userMap.get("id").toString());
        order.setUserId(userId);
        order.setCreatedAt(java.time.LocalDateTime.now());
        order.setUpdatedAt(java.time.LocalDateTime.now());
        // 生成订单编号（简单示例）
        order.setOrderNo("OD" + System.currentTimeMillis());
        // 设置初始状态为1-已预约
        order.setStatus(1);
        // 这里可以根据业务设置totalAmount等字段
        order.setTotalAmount(88.0); // TODO: 可根据套餐名动态设置
        orderService.createOrder(order);
        return Result.success(order);
    }
} 